/**
* Desc: 公共库文件-定义公共函数库
* User: cuixiaonai
* Email: 15965873315@163.com
* Date: 2022/1/10 11:49 AM
 */
package util

import (
	"bytes"
	config2 "go_gin_mvc/src/config"
	"gopkg.in/gomail.v2"
	"math/rand"
	"os"
	"regexp"
	"strings"
	"time"
)

/**
* Desc: 获取RSA公钥路径
* User: cuixiaonai
* Email: 15965873315@163.com
* Date: 2022/1/10 11:30 AM
 */
func GetRsaPublicPath() string {
	str, _ := os.Getwd()
	return str + "/src/config/keys/public.pem"
}

/**
* Desc: 随机字符串
* User: cuixiaonai
* Email: 15965873315@163.com
* Date: 2022/1/10 11:30 AM
 */
func RandChar(size int) string {
	const char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

	rand.NewSource(time.Now().UnixNano()) // 产生随机种子
	var s bytes.Buffer
	for i := 0; i < size; i++ {
		s.WriteByte(char[rand.Int63()%int64(len(char))])
	}
	return s.String()
}

/**
* Desc: 邮件发送
* User: cuixiaonai
* Email: 15965873315@163.com
* Date: 2022/1/10 11:30 AM
 */
func SendMail(mailAddress []string, subject string, body string) error {
	MailConfig := config2.Config().Mail

	m := gomail.NewMessage()
	m.SetHeader("From", m.FormatAddress(MailConfig.MailUser, MailConfig.UserAlias))
	m.SetHeader("To", mailAddress...) //发送给多个用户
	m.SetHeader("Subject", subject)   //设置邮件主题
	m.SetBody("text/html", body)      //设置邮件正文

	d := gomail.NewDialer(MailConfig.MailHost, MailConfig.MailPort, MailConfig.MailUser, MailConfig.MailPwd)

	err := d.DialAndSend(m)
	return err

	// 发送邮箱
	//err1 := util.SendMail([]string{"cuifang5513@qq.com"}, "你好", "这是golang测试邮件")
	//	if err1 != nil {
	//		logging.Error("邮件发送失败", err1)
	//		panic(err1)
	//	}
}

//获取Ip地址详细信息
func GetIpAddress(ip string) map[string]interface{} {

	IpAddress := make(map[string]interface{})

	return IpAddress
}

// 是否是email
func IsEmail(email string) bool {
	if email == "" {
		return false
	}
	ok, _ := regexp.MatchString(`^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[0-9a-zA-Z]{2,3}$`, email)
	return ok
}

// 按字节截取字符串 utf-8不乱码
func Substr(str string, length int64) string {
	bs := []byte(str)[:length]
	bl := 0
	for i := len(bs) - 1; i >= 0; i-- {
		switch {
		case bs[i] >= 0 && bs[i] <= 127:
			return string(bs[:i+1])
		case bs[i] >= 128 && bs[i] <= 191:
			bl++
		case bs[i] >= 192 && bs[i] <= 253:
			cl := 0
			switch {
			case bs[i]&252 == 252:
				cl = 6
			case bs[i]&248 == 248:
				cl = 5
			case bs[i]&240 == 240:
				cl = 4
			case bs[i]&224 == 224:
				cl = 3
			default:
				cl = 2
			}
			if bl+1 == cl {
				return string(bs[:i+cl])
			}
			return string(bs[:i])
		}
	}
	return ""
}

//Html过滤
func Html2str(html string) string {
	src := string(html)

	//替换HTML的空白字符为空格
	re := regexp.MustCompile(`\s`) //ns*r
	src = re.ReplaceAllString(src, " ")

	//将HTML标签全转换成小写
	re, _ = regexp.Compile("\\<[\\S\\s]+?\\>")
	src = re.ReplaceAllStringFunc(src, strings.ToLower)

	//去除STYLE
	re, _ = regexp.Compile("\\<style[\\S\\s]+?\\</style\\>")
	src = re.ReplaceAllString(src, "")

	//去除SCRIPT
	re, _ = regexp.Compile("\\<script[\\S\\s]+?\\</script\\>")
	src = re.ReplaceAllString(src, "")

	//去除所有尖括号内的HTML代码，并换成换行符
	re, _ = regexp.Compile("\\<[\\S\\s]+?\\>")
	src = re.ReplaceAllString(src, "\n")

	//去除连续的换行符
	re, _ = regexp.Compile("\\s{2,}")
	src = re.ReplaceAllString(src, "\n")

	return strings.TrimSpace(src)
}
